package com.doudanhua.basecode.jackson.configurer;

import com.doudanhua.basecode.jackson.time.DateTimeConverter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author huyang
 * @since 2020/05/28
 */
public class LocalDateTimeModelSerializer extends JsonSerializer<LocalDateTime> {
    private DateTimeConverter _dateConverter;

    public LocalDateTimeModelSerializer(DateTimeConverter dateConverter) {
        this._dateConverter = dateConverter;
    }


    @Override
    public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        String result;
        if (value == null)
            result = "";
        else
            result = _dateConverter.format(value);
        gen.writeString(result);
    }
}
